package com.xuan.shop.interceptor;

import com.xuan.shop.exception.InvildUserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class AuthorityInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        ArrayList<LinkedHashMap> ps = (ArrayList<LinkedHashMap>) redisTemplate.opsForValue().get(request.getParameter("token"));//获取用户的权限
        List<Object> list = new ArrayList<>();

        for (LinkedHashMap p : ps) {
            list.add(p.get("name"));
        }
        System.out.println(list);
        if (ps == null) {
            throw new InvildUserException("用户没有登陆");
        } else if (list.contains("findAll")) {
            System.out.println(ps);
            return true;
        } else {
            throw new InvildUserException("用户权限不足");

        }

    }
}
